﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace DailyDiet.Services.Navigation
{
    public class ContentNavigationService: IContentNavigationService
    {
        //私有变量
        private Shell _shell;

        private IContentPageActivationService _contentPageActivationService;

        public Shell Shell =>
            _shell ?? (_shell =  Shell.Current);

        /// <summary>
        /// 导航到指定页
        /// </summary>
        /// <param name="pageKey"></param>
        /// <returns></returns>
        public async Task NavigateToAsync(string pageKey) {
            var page = _contentPageActivationService.Activate(pageKey);
            if (page.Parent != null) {
                await page.Navigation.PopToRootAsync();
            }
            await Shell.Navigation.PushAsync(page);
        }
            
        /// <summary>
        /// 导航到自定页 带参数
        /// </summary>
        /// <param name="pageKey"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task NavigateToAsync(string pageKey, object param) {
            var page = _contentPageActivationService.Activate(pageKey);
            NavigationConstant.SetParameter(page, param);
            if (page.Parent != null)
            {
                await page.Navigation.PopToRootAsync();
            }
            await Shell.Navigation.PushAsync(page);
        }

        //构造函数
        public ContentNavigationService(IContentPageActivationService contentNavigationService) {
            _contentPageActivationService = contentNavigationService;
        }
    }
}
